﻿/*********************************************************************
 *
 *                             NOTICE
 *               COPYRIGHT (c) 2012 Panoscopix Software, Inc.
 *                UNPUBLISHED - ALL RIGHTS RESERVED
 * 
 *      This program is confidential, proprietary, and a trade
 *   secret of Panoscopix Software, Inc. The receipt or possession of
 *     this program does not convey any rights to reproduce or
 *      disclose its contents, or to manufacture, use, or sell
 *       anything that it may describe, in whole or in part, 
 *   without the specific written consent of Panoscopix Software, Inc.
 *
 * A set of utility methods for manipulating strings.
 * 
 *******************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FileSystem.Utility
{
	class StringUtil
	{
		public static String firstToken(String input) {
			return firstToken(input, ' ');
		}
	
		public static String firstToken(String input, char separator) {
			if (String.IsNullOrEmpty(input)) {
				return "";
			}
			int index = input.IndexOf(separator);
			if (-1 == index) {
				return input;
			}
			return input.Substring(0, index).Trim(); //up to but not including index.
		}
	
		public static String remainder(String input) {
			return remainder(input, ' ');
		}
	
		public static String remainder(String input, char separator) {
			if (String.IsNullOrEmpty(input))
			{
				return "";
			}
			int index = input.IndexOf(separator);
			if (-1 == index) {
				return "";
			}
			return input.Substring(index+1).Trim(); //index+1 to end of string.
		}
	
		public static String firstChar(String input) {
			if ((null == input) || (input.Length <= 1)) return input;
			return input[0].ToString();
		}
	
		public static String redact(String input) {
			if ((null == input) || (input.Length <= 1)) return input;
			StringBuilder rc = new StringBuilder();
			rc.Append(firstChar(input));
			for (int i = 1; i < input.Length; i++) rc.Append("*");
			return rc.ToString();
		}
	
		public static String pigLatin(String input) {
			if ((null == input) || (input.Length <= 1)) return input;
			StringBuilder rc = new StringBuilder();
			rc.Append(input.Substring(1));
			rc.Append(firstChar(input));
			rc.Append("ay");
			return  rc.ToString();
		}
	
		public static String toLower(String input) {
			if (String.IsNullOrEmpty(input)) return input;
			return input.ToLower();
		}
	
		public static String toUpper(String input) {
			if (String.IsNullOrEmpty(input)) return input;
			return input.ToUpper();
		}
	
		public static String display(String input) {
			if (String.IsNullOrEmpty(input)) return input;
			ConsoleOutUtil.println(input);
			return input;
		}

		public static bool isEmpty(String s) {
			return s.Length == 0;
		}

		public static bool isNullOrEmpty(String s) {
			return String.IsNullOrEmpty(s);
		}

		public static bool equals(String s1, String s2) {
			return s1.Equals(s2);
		}

		public static bool startsWith(String s, String prefix) {
			return s.StartsWith(prefix);
		}

		public static bool contains(String s, String substring) {
			return s.Contains(substring);
		}

		public static String substring(String s, int beginIndex) {
			return s.Substring(beginIndex);
		}
	}
}
